package com.xqboss.apps.service.synthesis;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.xqboss.apps.domain.nft.Nft;
import com.xqboss.apps.dto.synthesis.SynthesisBoxRuleSaveDto;
import com.xqboss.apps.service.nft.NftService;
import com.xqboss.common.core.domain.entity.SysUser;
import com.xqboss.common.exception.ServiceException;
import com.xqboss.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.synthesis.SynthesisBoxRule;
import com.xqboss.apps.mapper.synthesis.SynthesisBoxRuleMapper;


/**
 * 合成活动-宝盒规则(SynthesisBoxRule)表服务实现类
 *
 * @author makejava
 * @since 2024-02-04 13:43:48
 */
@Service
public class SynthesisBoxRuleService extends ServiceImpl<SynthesisBoxRuleMapper, SynthesisBoxRule> {

    @Autowired
    private NftService nftService;

    @Autowired
    private ISysUserService sysUserService;

    public boolean saveOrUpdateV2(SynthesisBoxRuleSaveDto param) {
        // 验证userids和nftids是否存在
        if (StrUtil.isNotBlank(param.getUserIds())) {
            String[] userIds = param.getUserIds().split(",");
            long existCount = sysUserService.lambdaQuery().in(SysUser::getUserId, userIds).count();
            if (userIds.length != existCount) {
                throw new ServiceException("有用户id不存在！");
            }
        }
        if (StrUtil.isNotBlank(param.getNftIds())) {
            String[] nftIds = param.getNftIds().split(",");
            long existCount = nftService.lambdaQuery().in(Nft::getId, nftIds).count();
            if (nftIds.length != existCount) {
                throw new ServiceException("有藏品id不存在！");
            }
        }
        return this.saveOrUpdate(BeanUtil.copyProperties(param, SynthesisBoxRule.class));
    }
}
